<template>
  <div class="product">
    <div class="addproduct">
      <el-button type="primary" @click="showcreatproduct">增加产品</el-button>
    </div>
    <div class="productList" v-show="tohide">
      <el-table
        :data="
          tableData.filter(
            (data) =>
              !search || data.name.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%"
      >
        <el-table-column label="Date" prop="id"> </el-table-column>
        <el-table-column label="产品名称" prop="name"> </el-table-column>
        <el-table-column label="产品图片">
          <template slot-scope="scope">
            <img
              :src="scope.row.img"
              alt="这是图片"
              style="width: 30px; height: 30px"
            />
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >Edit</el-button
            > -->
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="addproductbox" v-show="toshow">
      <div class="closebox" @click="hidecreateproduct">x</div>
      <createproduct></createproduct>
    </div>
  </div>
</template>

<script>
import createproduct from "../components/createproduct.vue"
import { getProductsList, deleteproductFunc } from "../http/request";
export default {
  name: "product",
  components:{
    createproduct
  },
  data() {
    return {
      toshow: false,
      tohide: true,
      tableData: [
        // {
        //   date: "2016-05-02",
        //   name: "防腐木材",
        //   address: "上海市普陀区金沙江路 1518 弄",
        //   url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
        // },
      ],
      search: "",
    };
  },
  methods: {
    showcreatproduct() {
      this.toshow = true;
      this.tohide = false;
    },
    hidecreateproduct() {
      this.toshow = false;
      this.tohide = true;
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.deleteproduct(row.id);
      this.$router.go(0);
    },
    // 获取产品列表
    async getproduct() {
      await getProductsList("/product/list").then((data) => {
        console.log(data);
        this.tableData = data.result;
      });
    },
    // 删除产品
    async deleteproduct(Id) {
      await deleteproductFunc("/product/deleteproduct", { id: Id }).then(
        (data) => {
          console.log(data);
        }
      );
    },
  },
  mounted() {
    this.getproduct();
  },
};
</script>

<style lang="scss" scoped>
.addproductbox {
  background-color: #fff;
  position: relative;
  .closebox {
    position: absolute;
    right: 15px;
    top: 10px;
    cursor: pointer;
  }
}
</style>